package smartclass.dao;

import java.util.ArrayList;

import smartclass.data.ManagerDataBaseHelper;
import smartclass.data.TableItem;
import smartclass.entity.DirectionInfo;
import smartclass.present.LogInActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DirectionDataDao {

	static DirectionDataDao directionDao;
	ManagerDataBaseHelper db;
	String tableName;

	public DirectionDataDao() {
		// TODO Auto-generated constructor stub
		db = LogInActivity.db;
		tableName = ManagerDataBaseHelper.DIRECTION_TABLE;
	}

	public static DirectionDataDao getInstance() {
		if (directionDao == null) {
			directionDao = new DirectionDataDao();
		}
		return directionDao;
	}

	//Add Item
	public boolean addDirection(DirectionInfo dir) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		if (String.valueOf(dir.getIdHomework()).isEmpty()) {
			return false;
		}
		ContentValues value = new ContentValues();
		value.put(TableItem.NAME, dir.getName());
		value.put(TableItem.DATE_CREAT, dir.getDateCreate());
		value.put(TableItem.ID_HOMEWORK, dir.getIdHomework());
		value.put(TableItem.PATH, dir.getPath());
		data.insert(tableName, null, value);
		data.close();
		return true;
	}

	//Get Item
	public DirectionInfo getDirection(int id) {
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		DirectionInfo dirInfo = new DirectionInfo();
		Cursor c = null;

		try {
			String[] params = new String[] { String.valueOf(id) };
			String sql = "SELECT * FROM " + tableName + " WHERE "
					+ TableItem.ID + "=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			dirInfo.setId(Integer.parseInt(c.getString(0)));
			dirInfo.setName(c.getString(1));
			dirInfo.setDateCreate(c.getString(2));
			dirInfo.setIdHomework(Integer.parseInt(c.getString(3)));
			dirInfo.setPath(c.getString(4));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return dirInfo;
	}

	//Get All Item
	public ArrayList<DirectionInfo> getAllDirection() {
		ArrayList<DirectionInfo> allDirection = new ArrayList<DirectionInfo>();
		Cursor c = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		String sql = "SELECT * FROM " + tableName;
		c = data.rawQuery(sql, null);
		DirectionInfo dirInfo = null;
		if (c.moveToFirst()) {
			do {
				dirInfo = new DirectionInfo();
				dirInfo.setId(Integer.parseInt(c.getString(0)));
				dirInfo.setName(c.getString(1));
				dirInfo.setDateCreate(c.getString(2));
				dirInfo.setIdHomework(Integer.parseInt(c.getString(3)));
				dirInfo.setPath(c.getString(4));
			} while (c.moveToNext());
		}
		return allDirection;
	}

	//Edit Item
	public boolean updateDirection(DirectionInfo dir) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		ContentValues values = new ContentValues();
		values.put(TableItem.NAME, dir.getName());
		values.put(TableItem.DATE_CREAT, dir.getDateCreate());
		values.put(TableItem.ID_HOMEWORK, dir.getIdHomework());
		values.put(TableItem.PATH, dir.getPath());

		data.update(tableName, values, TableItem.ID + "=?",
				new String[] { String.valueOf(dir.getId()) });
		data.close();
		return true;
	}

	//Delete Item
	public boolean deleteDirection(int id) {
		String sql = "DELETE FROM " + tableName + " WHERE " + TableItem.ID
				+ "=" + id;
		db.getWritableDatabase().execSQL(sql);
		return true;
	}

}
